package pattern.controller;

import com.google.common.collect.Lists;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import pattern.annontations.Secure;
import pattern.constant.WebSocketConstant;
import pattern.model.mq.SendMessageManyModel;
import pattern.model.mq.SendMessageModel;
import pattern.model.mq.SendMessageTokenModel;
import pattern.model.mq.SendMessageTopicModel;
import pattern.result.ResponseData;
import pattern.result.SocketResult;
import pattern.socketimpl.SocketSendProxy;

/**
 * 供其它微服务feign调用
 * @author Simon
 */
@Secure
@RestController
@RequiredArgsConstructor(onConstructor_ = @Autowired)
public class SocketController {
    private final SocketSendProxy socketSendProxy;

    /**
     * 发送客户端测试
     */
    @GetMapping(value = "/sendtest")
    public Boolean send() {
//        socketSendProxy.sendMsgByTopic(WebSocketConstant.SESSION_PUSH_SERVER,new ResponseData<>("发送至客户端主题订阅"));
        socketSendProxy.toOneByUserId(Lists.newArrayList(8888), WebSocketConstant.SESSION_PUSH_SERVER, new ResponseData<>("发送至客户端指定用户"));
        return Boolean.TRUE;
    }

    /**
     * 根据用户UUID发送消息
     */
    @PostMapping(value = "/sendByUUID")
    public SocketResult sendByUUID(@RequestBody SendMessageModel model) {
        model.setTopic(StringUtils.isNotBlank(model.getTopic()) ? model.getTopic() : WebSocketConstant.SESSION_PUSH_SERVER);
        return socketSendProxy.toOneByUUID(model.getUuid(), model.getTopic(), new ResponseData<>(model.getMsgBody()));
    }

    /**
     * 根据用户ID发送消息
     */
    @PostMapping(value = "/sendByUser")
    public SocketResult sendByUser(@RequestBody SendMessageModel model) {
        model.setTopic(StringUtils.isNotBlank(model.getTopic()) ? model.getTopic() : WebSocketConstant.SESSION_PUSH_SERVER);
        return socketSendProxy.toOneByUserId(Lists.newArrayList(model.getUserIds()), model.getTopic(), new ResponseData<>(model.getMsgBody()));
    }

    /**
     * 根据用户类型发送消息
     */
    @PostMapping(value = "/sendByUserType")
    public SocketResult sendByUserType(@RequestBody SendMessageManyModel model) {
        return socketSendProxy.toMany(model.getUserTypeEnum(), model.getTopic(), model.getResponseData());
    }

    /**
     * 根据订阅主题发送消息
     */
    @PostMapping(value = "/sendByTopic")
    public void sendByTopic(@RequestBody SendMessageTopicModel model) {
        socketSendProxy.sendMsgByTopic(model.getTopic(), model.getResponseData());
    }

    /**
     * 速度最快发送消息根据token
     */
    @PostMapping(value = "/sendByToken")
    public void sendByToken(@RequestBody SendMessageTokenModel model) {
        socketSendProxy.toOneByToken(model.getToken(), model.getTopic(), model.getResponseData());
    }
}
